package controller

import (
	"github.com/labstack/echo"
	"github.com/sirupsen/logrus"
	"net/http"
	"src/config"
	"src/util/context"
	"src/util/log"
	"strconv"
	"strings"
)

var lineLogger = log.GetLogger()

func writelineLoggerrLoggerLog(funcName, errMsg string, err error) {
	lineLogger.WithFields(logrus.Fields{
		"package":  config.LogMessage,
		"file":     "Line.go",
		"function": funcName,
		"err":      err,
	}).Warn(errMsg)
}

func Getlines(c echo.Context) error {
	idstring := c.QueryParam("routeid")
	ids := strings.Split(idstring, ",")
	var id []int
	for _, j := range ids {
		i, err := strconv.Atoi(j)
		if err != nil {
			writelineLoggerrLoggerLog("Getlines", err.Error(), err)
			return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)

		}
		id = append(id, i)
	}
	var data []config.Line
	for _, i := range id {
		if i <= len(config.LineS.Data) {
			data = append(data, config.LineS.Data[i-1])
		}
	}
	return context.RetData(c, data)
}
